package com.bcd.auctions;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bcd.beans.Article;
import com.bcd.beans.UserBean;
import com.bcd.model.ArticleManager;
import com.bcd.model.ArticleType;
import com.bcd.model.UserManager;

/**
 * Servlet implementation class Admin
 */
public class Admin extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
	
	private UserManager _users;
	private ArrayList<UserBean> _userList;
	private UserBean _user;
	
	private ArticleManager _articles;	
	private ArrayList<Article> _articleList;
	private Article _article;
	
	private RequestDispatcher _dispatcher;
    private HttpSession _session;
    private String _dispatcher_target;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin() {
        super();
        _users = new UserManager();
        _articles = new ArticleManager();
        _dispatcher_target = "admin.jsp";
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		_session = request.getSession(true);
		String action = request.getParameter("action");
		String show_page = "articles";
		
		if(action != null){
			if(action.equals("profile")){
				_session.setAttribute("current_user",getServletContext().getAttribute("current_user"));
				show_page="profile";				
			}else if(action.equals("articles")){				
				this.FetchArticleList(ArticleType.ALL);
				_session.setAttribute("articleList", _articleList);
				show_page = "articles";				
			}else if(action.equals("article")){				
				this.FetchArticle(Integer.parseInt(request.getParameter("id")));
				_session.setAttribute("article", _article);
				show_page = "article";				
			}else if(action.equals("users")){				
				this.FetchUserList();
				_session.setAttribute("userList", _userList);
				show_page = "users";				
			}else if(action.equals("user")){				
				this.FetchUser(Integer.parseInt(request.getParameter("id")));
				_session.setAttribute("user", _user);
				show_page = "user";				
			}
			else if(action.equals("edit_user")){				
				this.FetchUser(Integer.parseInt(request.getParameter("id")));
				_session.setAttribute("user", _user);
				show_page = "edit_user";				
			}
			else if(action.equals("save_user")){
				this.FetchUser(Integer.parseInt(request.getParameter("id")));
				String param = "";

				param = (String)request.getParameter("user_email");
				if(!param.equals(_user.getEmail())){
					_user.setEmail(request.getParameter("user_email").toString());
				}

				param = (String)request.getParameter("user_password");
				if(!param.equals(_user.getPassword())){
					_user.setPassword(request.getParameter("user_password").toString());
				}

				param = (String)request.getParameter("user_street");
				if(!param.equals(_user.getStreet())){
					_user.setStreet(request.getParameter("user_street").toString());
				}

				param = (String)request.getParameter("user_plz");
				if(!param.equals(_user.getPlz())){
					_user.setPlz(request.getParameter("user_plz").toString());
				}

				param = (String)request.getParameter("user_city");
				if(!param.equals(_user.getCity())){
					_user.setCity(request.getParameter("user_city").toString());
				}

				param = (String)request.getParameter("user_country");
				if(!param.equals(_user.getCountry())){
					_user.setCountry(request.getParameter("user_country").toString());
				}

				param = (String)request.getParameter("user_fname");
				if(!param.equals(_user.getFirstName())){
					_user.setFirstName(request.getParameter("user_fname").toString());
				}

				param = (String)request.getParameter("user_lname");
				if(!param.equals(_user.getLastName())){
					_user.setLastName(request.getParameter("user_lname").toString());
				}
				
				this.SaveUser();
				this.FetchUser(Integer.parseInt(request.getParameter("id")));
				_session.setAttribute("user", _user);
				show_page = "edit_user";				
			}
		}
		
		_session.setAttribute("show_page", show_page);
		_dispatcher = request.getRequestDispatcher(_dispatcher_target);
		_dispatcher.forward( request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private void FetchArticleList(ArticleType type){
		_articleList = _articles.GetArticleList(type);
	}
	private void FetchArticle(int article_id){
		_article = _articles.GetArticle(article_id);
	}
	private void FetchUserList(){
		_userList = _users.GetUserList();
	}
	private void FetchUser(int user_id){
		_user = _users.GetUser(user_id);
	}
	private void SaveUser(){
		_users.SaveUser(_user);
	}
}